package com.zx.callback.service.impl;

import com.zx.callback.common.enums.MessageEnumTable;
import com.zx.callback.common.enums.UsefulStateEnum;
import com.zx.callback.common.ex.JsonResponseException;
import com.zx.callback.common.handler.PageBean;
import com.zx.callback.dao.UserInfoMapper;
import com.zx.callback.po.UserInfo;
import com.zx.callback.service.UserInfoService;
import com.zx.callback.vo.PageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：yanst
 * @since: 2017/11/29
 * @history：(date editor desc)
 */
@Service
public class UserInfoServiceImpl extends BaseService implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public PageBean<UserInfo> pageList(UserInfo userInfo, PageVO pageVO) {
        PageHelper.startPage(pageVO.getCurPage(), pageVO.getPageSize());
        List<UserInfo> list = userInfoMapper.queryAll(userInfo);
        return pageBean((Page) list);
    }

    @Override
    public Boolean updateState(Long userId) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
        if (userInfo == null){
            logger.error("用户信息不存在!");
            throw new JsonResponseException(MessageEnumTable.USER_NOT_EXIST);
        }
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setId(userId);
        if (UsefulStateEnum.ENABLE.getKey() == userInfo.getFreezeFlag().byteValue()){
            userInfo1.setFreezeFlag(UsefulStateEnum.DISABLE.getKey());
        }else {
            userInfo1.setFreezeFlag(UsefulStateEnum.ENABLE.getKey());
        }

        int result = userInfoMapper.updateByPrimaryKeySelective(userInfo1);
        if (result < 1){
            logger.error("更改用户状态失败!");
            throw new JsonResponseException(MessageEnumTable.UPDATE_SQL);
        }
        return Boolean.TRUE;
    }

}
